package com.xyh.transaction.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@ApiModel(value = "响应结果")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty("响应文本")
    private String msg;

    @ApiModelProperty("响应数据")
    private T data;

    public ResponseResult(ResponseCode responseCode, T newToken) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getDetail();
        this.data = newToken;
    }

    // 工厂方法，创建操作成功的 ResponseResult
    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(200, message, data);
    }

    // 工厂方法，创建操作失败的 ResponseResult
    public static <T> ResponseResult<T> failure(int code, String message) {
        return new ResponseResult<>(code, message, null);
    }

    public static <T> ResponseResult<T> failure(ResponseCode responseCode) {
        return new ResponseResult<>(responseCode.getCode(), responseCode.getDetail(), null);
    }
}
